class DownLoadVideo {
	constructor() {
		this.list = []

		let el = document.querySelector('.XYnWH9QO')
		let title = el.querySelector('#video-title')
		if (title) {
			title.innerHTML = ''
		} else {
			title = document.createElement('div')
			title.id = "video-title"
			el.append(title)
		}
		this.elContent = title
		this.addButton() 
		// this.fetchVideo()
	}
	addButton(){
		let el = document.querySelector('.WPSxa4XK')
		el.innerHTML = ''
		el.style.cursor = 'pointer'
		el.style.color = '#FFF'
		el.style.padding = '0px 10px'
		el.style.margin = '2px'
		el.style.border = '1px solid #808080'
		el.innerText = "更新"
		el.onclick=()=>{
			this.current()
		}
	}
	fetchVideo() {
		let videos = document.getElementsByTagName('video')
		let video = videos[0].children[0]
		let url = video.getAttribute('src')
		this.list.push(url)
		window.open(url)
	}
	current() {
		let videos = document.getElementsByTagName('video')
		let video = videos[0].children[0]
		let url = video.getAttribute('src')
		this.elContent.innerHTML = ''
		this.createDownload(url)
		this.fetchTitle()
		// window.open(url)
	}
	createDownload(url) {
		let tag = document.createElement('span')
		tag.style.cursor = 'pointer'
		tag.style.color = '#FFF'
		tag.style.padding = '0px 10px'
		tag.style.margin = '2px'
		tag.style.border = '1px solid #808080'
		tag.innerText = '下载视频'
		tag.onclick = () => {
			this.download(url)
		}
		this.elContent.append(tag)
	}
	async download(url = '') {
		let response = await fetch(url)
		let data = await response.blob()
		let href = window.URL.createObjectURL(data)
		let el = document.createElement('a')
		el.href = href
		let title = await navigator.clipboard.readText()
		if (!title) {
			title = "抖音视频"
		}
		el.download = `${title}.mp4`
		document.body.append(el)
		el.click()
		window.URL.revokeObjectURL(href)
		setTimeout(() => {
			el.remove()
		}, 1000)

	}
	fetchTitle() {
		let el = document.querySelector('.hE8dATZQ')
		let contents = el.innerText
		let list = contents.split('#')
		// console.table(list)
		this.appendTitle(list)
	}
	appendTitle(list = []) {
		for (let i of list) {
			let tag = document.createElement('span')
			tag.style.cursor = 'pointer'
			tag.style.color = '#FFF'
			tag.style.padding = '0px 10px'
			tag.style.margin = '2px'
			tag.style.border = '1px solid #808080'
			tag.innerText = i.trim()
			tag.onclick = () => {
				navigator.clipboard.writeText(tag.innerText)
				console.log('已复制文本：' + tag.innerText)
			}
			this.elContent.appendChild(tag)
		}
	}

}


let ctx = new DownLoadVideo()